package com.kaka.bus.engine.home.main.h5;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.SimpleActivity;
import com.kaka.bus.util.StatusBarUtil;
import com.kaka.bus.util.U;

import butterknife.BindView;

public class OtherH5Activity extends SimpleActivity {
    @BindView(R.id.toolbar_name)
    TextView toolbar_name;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.webview_progressbar)
    ProgressBar mWebviewProgressbar;
    @BindView(R.id.webview)
    WebView mWebview;
    public static final String URL = "url";
    public static final String TITLE = "title";
    private String title;
    private String url;

    @Override
    protected void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra(TITLE);
        url = intent.getStringExtra(URL);
        toolbar_name.setText(title);
        U.initWebView(mWebview, mWebviewProgressbar);
        mWebview.loadUrl(url);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_other_h5;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initListener() {
        super.initListener();
        toolbar_back.setOnClickListener(v -> {
            if (mWebview.canGoBack()) {
                //获取webView的浏览记录
                WebBackForwardList mWebBackForwardList = mWebview.copyBackForwardList();
                //这里的判断是为了让页面在有上一个页面的情况下，跳转到上一个html页面，而不是退出当前activity
                if (mWebBackForwardList.getCurrentIndex() > 0) {
                    String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex() - 1).getUrl();
                    if (!historyUrl.equals(url)) {
                        mWebview.goBack();
                    }
                } else {
                    finish();
                }
            } else {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mWebview.canGoBack()) {
                //获取webView的浏览记录
                WebBackForwardList mWebBackForwardList = mWebview.copyBackForwardList();
                //这里的判断是为了让页面在有上一个页面的情况下，跳转到上一个html页面，而不是退出当前activity
                if (mWebBackForwardList.getCurrentIndex() > 0) {
                    String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex() - 1).getUrl();
                    if (!historyUrl.equals(url)) {
                        mWebview.goBack();
                        return true;
                    }
                }
            } else {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebview != null) {

            // 如果先调用destroy()方法，则会命中if (isDestroyed()) return;这一行代码，需要先onDetachedFromWindow()，再
            // destory()
            ViewParent parent = mWebview.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mWebview);
            }

            mWebview.stopLoading();
            // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
            mWebview.getSettings().setJavaScriptEnabled(false);
            mWebview.clearHistory();
            mWebview.clearView();
            mWebview.removeAllViews();
            mWebview.destroy();

        }

    }
}
